package com.epfresh.supplier.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epfresh.api.adapter.CommonAdapter;
import com.epfresh.api.adapter.ViewHolder;
import com.epfresh.api.constant.AppSupplyRouterConstant;
import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.http.HttpRequest;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.T;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.LogisticsInfoEntity;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderLogisticsInfoActivity  extends BaseActivity {

    static final String TAG = "OrderLogisticsInfoActivity";
    String logisticsNo,phone;

    TextView tvPoster,tvNumber,tvTime,tvUrge;
    TextView tvReceiver,tvReceiverPhone,tvAddress,tvPayType;
    ListView lv;
    ArrayList<LogisticsInfoEntity.ListBean> list = new ArrayList<>();
    LogisticsAdapter logisticsAdapter;
    private RelativeLayout rlInfo,rlReceiverInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        setToolbarTitle("订单跟踪");
        logisticsNo = getIntent().getStringExtra("logisticsNo");
        if(logisticsNo==null) {
            T.toast("数据有误，请重试");
            finish();
        }
        mHttpInfo();
    }
    private void initView() {
        setContentView(R.layout.activity_order_logisticsinfo);
        rlInfo=findViewById(R.id.rl_info);
        rlReceiverInfo=findViewById(R.id.rl_receiver_info);
        tvPoster = (TextView)findViewById(R.id.tv_poster);
        tvNumber = (TextView)findViewById(R.id.tv_number);
        tvTime = (TextView)findViewById(R.id.tv_time);
        tvReceiver = (TextView)findViewById(R.id.tv_receiver);
        tvReceiverPhone = (TextView)findViewById(R.id.tv_receiver_phone);
        tvReceiverPhone.setOnClickListener(this);
        tvAddress = (TextView)findViewById(R.id.tv_address);
        tvPayType = (TextView)findViewById(R.id.tv_pay_type);

        lv = (ListView)findViewById(R.id.lv);
        logisticsAdapter = new LogisticsAdapter(this,list,R.layout.item_money_and_logistics);
        lv.setAdapter(logisticsAdapter);
        lv.setDivider(null);
        baseback.setVisibility(View.VISIBLE);
        baseback.setOnClickListener(this);
        tvUrge = (TextView)findViewById(R.id.tv_urge);
        tvUrge.setOnClickListener(this);
    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.baseback:
                // 返回上一页
                finish();
                break;

            case R.id.tv_urge:
                Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));//调出来拨打页面；
                startActivity(call);
                break;
            case R.id.tv_receiver_phone:
                Intent phone = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + tvReceiverPhone.getText().toString()));//调出来拨打页面；
                startActivity(phone);
            default:
                break;
        }
    }
    class LogisticsAdapter extends CommonAdapter<LogisticsInfoEntity.ListBean> {
        public LogisticsAdapter(Context context, List<LogisticsInfoEntity.ListBean> historyList, int a) {
            super(context, historyList, a);
        }

        @Override
        public void convert(ViewHolder holder, final LogisticsInfoEntity.ListBean bean) {
            holder.setText(R.id.tv_content, bean.getSupplyInfo());
            holder.setText(R.id.tv_time, bean.getNodeTime());
            if(list.indexOf(bean)==list.size()-1){
                holder.getView(R.id.view_2).setVisibility(View.INVISIBLE);
            }
            if(list.indexOf(bean)==0){
                holder.getView(R.id.view_1).setVisibility(View.INVISIBLE);
                ((ImageView)holder.getView(R.id.iv_state)).setImageResource(R.mipmap.order_red_dot);
            }
        }
    }
    /** 查看物流  */
    private void mHttpInfo() {
        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(AppSupplyRouterConstant.cmd_order_appOrder_orderPathInfo);
        Map<String, String> map = new HashMap<>();
        map.put("orderId", logisticsNo);
        data.setParameters(map);
        request(data, AppSupplyRouterConstant.cmd_order_appOrder_orderPathInfo, new OnRequestListener<LogisticsInfoEntity>() {
            @Override
            public LogisticsInfoEntity jsonToObj(String responseStr) {
                return new Gson().fromJson(responseStr, LogisticsInfoEntity.class);
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {
                hideProgressDialog();
                showExceptionView(R.mipmap.upload_error, "网路错误", HttpRequest.FAIL_NET_FAULT);
            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {
                hideProgressDialog();
                showExceptionView(R.mipmap.upload_error, arg + "", HttpRequest.FAIL_NET_FAULT);
            }
            @Override
            public void onStart(Object tag) {
                showProgressDialog();
            }

            @Override
            public void onResponse(ResponseEntity<LogisticsInfoEntity> response, Object tag) {
                LogisticsInfoEntity responseElement = response.getResponseElement();
                updateOrderInfo(responseElement);
                hideProgressDialog();
            }

        });
    }
    void updateOrderInfo(LogisticsInfoEntity moneyPath){
        phone = moneyPath.getDriverPhone();
        if(moneyPath.getReceiver()==null||moneyPath.getReceiver().equals("")||moneyPath.getReceiver().equals("null")){
            moneyPath.setReceiver("无");
        }
        if(moneyPath.getReceiverPhone()==null||moneyPath.getReceiverPhone().equals("")||moneyPath.getReceiverPhone().equals("null")){
            moneyPath.setReceiverPhone("无");
        }
        if(moneyPath.getDistributionTitle()==null||moneyPath.getDistributionTitle().equals("")||moneyPath.getDistributionTitle().equals("null")){
            moneyPath.setDistributionTitle("无");
        }
        tvReceiverPhone.setText(moneyPath.getReceiverPhone());
        tvNumber.setText("订单："+moneyPath.getOrderNo());
        tvPoster.setText("配送方："+moneyPath.getDistributionTitle());
        tvPayType.setText("支付方式："+moneyPath.getPayTypeTitle());
        if(moneyPath.getDeliveryType()%2==1){//配送
            tvTime.setText("预计送达时间："+moneyPath.getDeliveryDateTime());
            tvReceiver.setText("收货人：" +moneyPath.getReceiver());
            tvAddress.setText("配送地址："+moneyPath.getReceiverAddress());
        }else {//自提
            tvTime.setText("提货时间："+moneyPath.getDeliveryDateTime());
            tvReceiver.setText("提货人：" +moneyPath.getReceiver());
            tvAddress.setVisibility(View.GONE);
        }
        list.addAll(moneyPath.getList());
        logisticsAdapter.notifyDataSetChanged();
        // 商家配送隐藏顶部的信息
        if (moneyPath.getDeliveryType() == 6) {
            rlInfo.setVisibility(View.GONE);
            rlReceiverInfo.setVisibility(View.GONE);
        } else {
            rlInfo.setVisibility(View.VISIBLE);
            rlReceiverInfo.setVisibility(View.VISIBLE);
        }
    }

}
